#include "comm.hpp"
#include <string.h>

int main()
{
    umask(0);
    int n = mkfifo(FIFO_FILE, 0660);
    if(n < 0)
    {
        std::cerr << "mkfifo error" << '\n';
        return 1;
    }

    //read
    int fd = open(FIFO_FILE, O_RDONLY);
    if(fd < 0)
    {
        std::cerr << "open fifo error" << strerror(errno)<< '\n';
        return 2;
    }
    std::cout << "open fifo success" << '\n';

    while(true)
    {

        char buffer[1024];
        int number = read(fd, buffer, sizeof(buffer) - 1);
        if(number > 0)
        {
            buffer[number] = 0;
            std::cout << "Client said#" << buffer << '\n';
        }
        else
        {
            
        }
    }

    close(fd);

    n = unlink(FIFO_FILE);
    if(n == 0)
    {
        std::cout << "remove fifo success" << '\n';
    }
    else 
    {
        std::cout << "remove fifo error" << '\n';
    }
    return 0;
}